#include "userapp.h"

static void display_task(void *arg);
static void imu_task(void *arg);
static void wifi_task(void *arg);

void usertask_create(void)
{
    xTaskCreate(display_task, "display_task", 1024 * 2, NULL, configMAX_PRIORITIES - 2, NULL);
    xTaskCreate(imu_task, "imu_task", 1024 * 2, NULL, configMAX_PRIORITIES - 3, NULL);
    //xTaskCreate(wifi_task, "wifi_task", 1024 * 2, NULL, configMAX_PRIORITIES - 4, NULL);
}

static void display_task(void *arg)
{
    static const char *DISPLAY_TASK_TAG = "DISPLAY_TASK";
    esp_log_level_set(DISPLAY_TASK_TAG, ESP_LOG_INFO);

    while(1)
    {
        if(ReadINT1)
        {
            HV_EN;

            sr_display(sr.state * 11, 1, 0, 0, 0);vTaskDelay(200 / portTICK_PERIOD_MS);
            sr_display(sr.state * 11, 1, 1, 0, 0);vTaskDelay(200 / portTICK_PERIOD_MS);
            sr_display(sr.state * 11, 1, 1, 1, 0);vTaskDelay(200 / portTICK_PERIOD_MS);
            sr_display(sr.state * 11, 1, 1, 1, 1);vTaskDelay(200 / portTICK_PERIOD_MS);
            sr.state ++;
            if(sr.state == 10)
            {
                sr.state = 0;
            }
            HV_SD;
        }
        adc2_get_raw(0, 3, &VBAT_ADCVal);
        vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}

static void imu_task(void *arg)
{
    static const char *IMU_TASK_TAG = "IMU_TASK";
    esp_log_level_set(IMU_TASK_TAG, ESP_LOG_INFO);

    while(1)
    {
        accel_read();
        vTaskDelay(300 / portTICK_PERIOD_MS);
    }
}

static void wifi_task(void *arg)
{
    static const char *WIFI_TASK_TAG = "WIFI_TASK";
    esp_log_level_set(WIFI_TASK_TAG, ESP_LOG_INFO);

    while(1)
    {
        vTaskDelay(300 / portTICK_PERIOD_MS);
    }
}

void display_time(void)
{
    // sr_display(rtc.getHour(),1,1,0,0);
    // vTaskDelay(700);
    // sr_display(rtc.getMinute(),0,0,1,1);
    // vTaskDelay(700);
    // sr_display(rtc.getHour(),1,1,0,0);
    // vTaskDelay(700);
    // sr_display(rtc.getMinute(),0,0,1,1);
    // vTaskDelay(700);
    // sr_display(rtc.getSecond(),1,0,0,0);
    // vTaskDelay(1000);
    // sr_display(rtc.getSecond(),0,1,0,0);
    // vTaskDelay(1000);
    // sr_display(rtc.getSecond(),0,0,1,0);
    // vTaskDelay(1000);
    // sr_display(rtc.getSecond(),0,0,0,1);
    // vTaskDelay(1000);
}

void display_voltage(void)
{
    adc2_get_raw(0, 3, &VBAT_ADCVal);
    float VB = (VBAT_ADCVal * 3.3 * 2) / 4096;

    VB *= 100 * 3;
    uint8_t VB_D0 = (uint16_t)VB / 100 % 10;
    uint8_t VB_D1 = (uint16_t)VB / 10 % 10;
    uint8_t VB_D2 = (uint16_t)VB % 10;
    
    sr_display(VB_D0 * 10 + VB_D1, 0, 1, 1, 0);
    delay(200);
    sr_display(VB_D0 * 10 + VB_D2, 0, 1, 0, 1);
    delay(200);
    sr_display(VB_D0 * 10 + VB_D1, 0, 1, 1, 0);
    delay(200);
    sr_display(VB_D0 * 10 + VB_D2, 0, 1, 0, 1);
    delay(200);
}

void nixie_fresh(void)
{
    sr_display(9, 0, 0, 0, 0);vTaskDelay(Fresh_Delay_Time);
    sr_display(98, 1, 0, 0, 0);vTaskDelay(Fresh_Delay_Time);
    sr_display(87, 1, 1, 0, 0);vTaskDelay(Fresh_Delay_Time);
    sr_display(76, 1, 1, 1, 0);vTaskDelay(Fresh_Delay_Time);
    sr_display(65, 1, 1, 1, 1);vTaskDelay(Fresh_Delay_Time);
    sr_display(54, 1, 1, 1, 1);vTaskDelay(Fresh_Delay_Time);
    sr_display(43, 1, 1, 1, 1);vTaskDelay(Fresh_Delay_Time);
    sr_display(32, 0, 1, 1, 1);vTaskDelay(Fresh_Delay_Time);
    sr_display(21, 0, 0, 1, 1);vTaskDelay(Fresh_Delay_Time);
    sr_display(10, 0, 0, 0, 1);vTaskDelay(Fresh_Delay_Time);
    sr_display(0, 0, 0, 0 , 0);vTaskDelay(Fresh_Delay_Time);
}